home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-30 | 8.1 KB | 346 lines | [TEXT/KAHL] |
- /***********************************************************************************
- CExpander.h
-
- Copyright © 1994 B-Ray Software. All rights reserved.
- Developed using Symantec C++ 7.0.2 and Symantec's TCL library.
- Portions of this code courtesy Symantec, Inc.
-
- This code may be freely distributed as long as this notice remains. This code
- may not be used in any commercial software without the consent of B-Ray Software.
-
- ---
-
- CExpander class provides functionality like the Apple's Finder list windows.
- There is a toggle button associated with each Expander, and clicking on it
- will expose/hide children managed by the Expander. This class does not handle
- selection of children; CExpanderList does.
-
- ***********************************************************************************/
- #include "CExpander.h"
- #include "CExpanderLabel.h"
- #include "CExpanderButton.h"
- #include "ExpanderMessages.h"
-
-
- TCL_DEFINE_CLASS_D1( CExpander, CExpanderPane );
-
-
- short const CExpander::kExpanderButtonSize = 16;
-
-
- /*
- * CExpander constructor
- *
- * Default constructor - should only be called when created by a file read.
- */
-
- CExpander :: CExpander() : CExpanderPane()
- {
- itsLabel = NULL;
- itsButton = NULL;
-
- isExpanded = FALSE;
- SetWantsClicks( TRUE ); // we want mouse clicks for our button
-
- TCL_END_CONSTRUCTOR
- }
-
-
- /*
- * CExpander constructor
- *
- * Normal constructor - should always be called when creating a new
- * CExpander object.
- */
-
- CExpander :: CExpander( CView *anEnclosure, CBureaucrat *aSupervisor,
- short aWidth, short aHeight, short aHLoc, short aVLoc,
- SizingOption aHSizing, SizingOption aVSizing )
- : CExpanderPane( anEnclosure, aSupervisor, aWidth, aHeight, aHLoc, aVLoc,
- aHSizing, aVSizing )
- {
- itsLabel = NULL;
- itsButton = NULL;
-
- isExpanded = FALSE;
- SetWantsClicks( TRUE ); // we want mouse clicks for our button
-
- TCL_END_CONSTRUCTOR
- }
-
-
- /*
- * CExpander destructor
- *
- * Just a place-holder for Inspector
- */
-
- CExpander :: ~CExpander()
- {
- TCL_START_DESTRUCTOR
-
- /*
- * Since the label and button objects are inherited from CPane, don't blow them away here.
- * Let CPane do it when it gets destroyed - CView disposes of all subviews.
- */
- itsLabel = NULL;
- itsButton = NULL;
- }
-
-
- void CExpander :: IExpander( void )
- {
- MakeExpanderButton();
- MakeExpanderLabel();
- AppendChild( itsLabel );
- }
-
-
- /*
- * MakeExpanderButton method
- *
- * Protected method to create CExpanderButton object.
- */
-
- void CExpander :: MakeExpanderButton( void )
- {
- short aWidth = kExpanderButtonSize;
- short aHeight = kExpanderButtonSize;
-
- /*
- * Create button. NOTE: we don't treat the button as a child. Otherwise, our label
- * would be positioned BELOW the button (CColumnizer behavior). We just treat this
- * as a normal pane, and offset the label so they won't overlap.
- */
- itsButton = TCL_NEW( CExpanderButton, ( this, this, aWidth, aHeight ) );
- }
-
-
- /*
- * MakeExpanderLabel method
- *
- * Protected method to create CExpanderLabel object.
- */
-
- void CExpander :: MakeExpanderLabel( void )
- {
- short aWidth = width - ( kExpanderButtonSize + 4 );
- short aHeight = kExpanderButtonSize + 4;
- short aHLoc = kExpanderButtonSize + 4;
- short aVLoc = 0;
-
- itsLabel = TCL_NEW( CExpanderLabel, ( this, this, aWidth, aHeight, aHLoc, aVLoc,
- sizFIXEDSTICKY, sizFIXEDSTICKY ) );
- }
-
-
-
- /*
- * SetMinizedSize method
- *
- * Protected method that sets the size of the pane to just the size of its header,
- * thus hiding all other children.
- */
-
- void CExpander :: SetMinimizedSize( void )
- {
- SetExpanderSize( itsLabel->width + kExpanderButtonSize + 4, itsLabel->height );
- }
-
-
- /*
- * SetExpandedSize method
- *
- * Protected method that sets the size of the pane to the size of all of its
- * children, making them all viewable.
- */
-
- void CExpander :: SetExpandedSize( void )
- {
- short aHeight, aWidth;
-
- GetFamilySize( &aWidth, &aHeight ); // get dimension of rectangle that encloses children
- SetExpanderSize( aWidth, aHeight ); // and set ourselves to that size
- }
-
-
- /*
- * SetExpandedState method
- *
- * Access method that changes our expansion size.
- */
-
- void CExpander :: SetExpandedState( Boolean fExpanded )
- {
- if ( fExpanded != isExpanded ) { // ignore repeat calls
- isExpanded = fExpanded; // record new value
- if ( isExpanded )
- SetExpandedSize(); // become bigger
- else
- SetMinimizedSize(); // become smaller
-
- ValidateHeader(); // remove header from update activity
-
- itsButton->SetValue( isExpanded ); // update our button to reflect change
- }
- }
-
-
- /*
- * ValidateHeader method
- *
- * Removes label and button panes from update activity caused by our changing sizes.
- * Helps reduce flicker caused by excessive redraws.
- */
-
- void CExpander :: ValidateHeader( void )
- {
- LongRect anAperture;
- Rect validRect;
-
- itsButton->Prepare();
- itsButton->GetAperture( &anAperture ); // get header's frame
- itsButton->FrameToQDR( &anAperture, &validRect ); // put into QD coords
- ValidRect( &validRect ); // remove from update region
-
- itsLabel->Prepare();
- itsLabel->GetAperture( &anAperture ); // get header's frame
- itsLabel->FrameToQDR( &anAperture, &validRect ); // put into QD coords
- ValidRect( &validRect ); // remove from update region
- }
-
-
- /*
- * ChangeSelf method - OVERRIDE
- *
- * Adjust ourself to reflect the changes of our children.
- */
-
- void CExpander :: ChangeSelf( long changedIndex, Rect *delta )
- {
- short aWidth, aHeight;
-
- if ( isExpanded ) {
- SetExpandedSize();
- }
- else {
- SetMinimizedSize();
- }
-
- /*
- * If there is no stuff to show when expanded, disable our button.
- */
- GetFamilySize( &aWidth, &aHeight ); // get dimension of rectangle that encloses children
- if ( GetNumberChildren() == 1 || aHeight == itsLabel->height ) {
- itsButton->SetEnabled( FALSE );
- if ( isExpanded ) {
- itsButton->SetValue( FALSE );
- }
- }
- else {
- itsButton->SetEnabled( TRUE );
- }
- }
-
-
- /*
- * DoCommand method - OVERRIDE
- *
- * Handles cmdExpandPane and cmdUnexpandPane messages that are sent from our button.
- * Others could send them too, but don't know why...
- */
-
- void CExpander :: DoCommand( long theCommand )
- {
- if ( theCommand == cmdExpandPane )
- SetExpandedState( TRUE );
- else if ( theCommand == cmdUnexpandPane )
- SetExpandedState( FALSE );
- else
- CExpanderPane::DoCommand( theCommand );
- }
-
-
- /*
- * DoKeyDown method - OVERRIDE
- *
- * Handles key down events. We only look for RETURN keystrokes
- */
-
- void CExpander :: DoKeyDown( char theChar, Byte keyCode, EventRecord *macEvent )
- {
- if ( theChar == kEnterKey || theChar == '\r' && itsButton->GetEnabled() ) {
- SetExpandedState( ! isExpanded );
- }
- else {
- CExpanderPane::DoKeyDown( theChar, keyCode, macEvent );
- }
- }
-
-
- /*
- * ParentMessage method - OVERRIDE
- *
- * Handles the kExpanderPrinting message from the parent. When our parent
- * pane is told to start printing, we want to be expanded so that all of our
- * children will be drawn. We remember the previous state of our expansion,
- * expand ourselves, and then wait for another kExpanderPrinting message to
- * set ourselves back to the saved state. The param value is treated as a
- * boolean value telling us if we are beginning or ending the printing phase.
- */
-
- void CExpander :: ParentMessage( long message, void *param )
- {
- if ( message == kExpanderPrinting ) { // family tree is being printed
- if ( param ) { // before printing
- savedExpanderFlag = isExpanded; // save present state
- SetExpandedState( TRUE ); // make sure we are expanded
- }
- else { // after printing
- SetExpandedState( savedExpanderFlag ); // reset to previous state
- }
- TellChildren( message, param ); // tell our children the same message
- }
- else {
- CExpanderPane::ParentMessage( message, param );
- }
- }
-
-
- /*
- * PutTo method - OVERRIDE
- *
- * Writes to the stream all of the info we need to save.
- */
-
- void CExpander :: PutTo( CStream &stream )
- {
- stream << isExpanded;
-
- CExpanderPane::PutTo( stream );
-
- stream << itsLabel;
- stream << itsButton;
- }
-
-
- /*
- * GetFrom method - OVERRIDE
- *
- * Reads from the stream all of the info that we saved.
- */
-
- void CExpander :: GetFrom( CStream &stream )
- {
- Boolean aFlag;
-
- stream >> aFlag;
-
- CExpanderPane::GetFrom( stream );
-
- itsLabel = (CExpanderLabel *)stream.GetView( this, this );
- itsButton = (CExpanderButton *)stream.GetView( this, this );
-
- SetExpandedState( aFlag );
- }
-